package org.water.sys.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.water.base.service.BaseService;
import org.water.common.db.DbAdapter;
import org.water.common.util.StringUtil;
import org.water.sys.entity.SysGroupRole;
import org.water.sys.entity.SysGroupUser;
import org.water.sys.entity.SysRole;
import org.water.sys.entity.SysGroup;
import org.water.sys.repository.RoleDao;
import org.water.sys.repository.UserGroupDao;
import org.water.sys.repository.UserGroupRoleDao;
import org.water.sys.repository.UserGroupUserDao;
/**
 * 用户组dao
 * @author qzy
 *
 */
@Service
public class UserGroupService extends BaseService<SysGroup,String> {

	/**
     * 用户组dao
     * @return
     */
	protected UserGroupDao getRepository()
	{
		return (UserGroupDao) super.baseRespository;
	}
	@Autowired
	private UserGroupRoleDao groupRoleDao;
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private UserGroupUserDao groupUserDao;
	/**
	 * 查询用户组
	 * @param roleName
	 * @param pageRequest
	 * @return
	 */
	public Page<SysGroup> findByGroupNameLike(String userGroupName,Pageable pageRequest)
	{
		return getRepository().findByGroupNameLike(userGroupName, pageRequest);
	}
	/**
	 * 查询用户，包括用户组权限
	 * @param userGroupName
	 * @param pageRequest
	 * @return
	 */
	public Page<Map> findByGroupName(String userGroupName,Pageable pageRequest)
	{
		Page<Map> results=null;
		ArrayList<String> params=new ArrayList<>();
		StringBuffer sql=new StringBuffer();
		sql.append("select ug.id,ug.group_name,ug.remark,gr.role_names ");
		sql.append(" from sys_group ug LEFT JOIN  ");
		sql.append(" ( select gr.group_id,"+DbAdapter.groupConcat("r.id")+" role_ids ,"+DbAdapter.groupConcat("r.role_name")+" role_names from  ");
		sql.append("sys_group_role gr,sys_role r where gr.role_id=r.id GROUP BY gr.group_id ) gr on ug.id=gr.group_id ");
		sql.append(" where 1=1 " );
		if(!StringUtil.isEmpty(userGroupName))
		{
			params.add("%"+userGroupName+"%");
			sql.append(" and ug.group_name like ? ");
		}
		
		results=getRepository().createNativeQueryToMap(sql.toString(), params.toArray(), pageRequest);
		return results;
	}
	/**
	 * 根据用户组查询用户
	 * @param groupId
	 * @return
	 */
	public List<Map> findUserByGroupId(String groupId)
	{
		List<Map> results=null;
		String[] params={groupId};
		StringBuffer sql=new StringBuffer();
		sql.append("select gu.id,u.user_name,u.alias_name from sys_user u,sys_group_user gu where u.id=gu.user_id and gu.group_id=? ");
		results=getRepository().createNativeQueryToMap(sql.toString(), params);
		return results;
	}
	/**
	 * 查询不包含在用户组中的角色
	 * @param groupId
	 * @return
	 */
	public List<SysRole> findRoleNotInGroup(String groupId)
	{
		return roleDao.findNotContainsGroupRole(groupId);
	}
	/**
	 * 查询组关联的角色
	 * @param groupId
	 * @return
	 */
	public List<SysGroupRole> findGroupRole(String groupId)
	{
		List<SysGroupRole> groupRoles=null;
		groupRoles=groupRoleDao.createBeanQuery(SysGroupRole.class).eq("groupId", groupId).list();
		return groupRoles;
	}
	/**
	 * 保存用户组关联角色
	 * @param groupRoles
	 */
	public void saveUserGroupRoles(String groupId,List<SysGroupRole> groupRoles)
	{
		groupRoleDao.deleteByGroupId(groupId);
		groupRoleDao.save(groupRoles);
	}
	/**
	 * 查询不包含在用户组中的用户
	 * @param groupId
	 * @return
	 */
	public Page<Map> findUserNotInGroup(String groupId,String userName,Pageable pageRequest)
	{
		Page<Map> results=null;
		HashMap<String, Object> params=new HashMap<>();
		params.put("groupId", groupId);
		StringBuffer sql=new StringBuffer();
		sql.append(" select u.id,u.user_name,u.alias_name,u.email,u.phone,u.remark ");
		sql.append(" from sys_user u where u.del_status=0 and u.id not in (select gu.user_id from sys_group_user gu where gu.group_id=:groupId) ");
		if(!StringUtil.isEmpty(userName))
		{
			sql.append(" and (u.user_name like :userName or u.alias_name like :userName) ");
			params.put("userName", "%"+userName+"%");
		}
		
		results=getRepository().createNativeQueryToMap(sql.toString(), params,pageRequest);
		return results;
	}
	/**
	 * 保存用户组关联用户
	 * @param groupUsers
	 */
	public void saveUserGroupUsers(List<SysGroupUser> groupUsers)
	{
		groupUserDao.save(groupUsers);
	}
	/**
	 * 删除用户组关联的用户
	 * @param ids
	 */
	public void deleteUserGroupUser(String[] ids)
	{
		groupUserDao.delete(ids);
	}
	/**
	 * 用户组是否关联用户
	 * @param groupId
	 * @return
	 */
	public boolean hasGroupRelUser(String groupId)
	{
		boolean f=false;
		Long c= this.groupUserDao.createBeanQuery(SysGroupUser.class).eq("groupId", groupId).count();
		if(c>0) f=true;
		return f;
	}
	/**
	 * 用户组是否关联角色
	 * @param groupId
	 * @return
	 */
	public boolean hasGroupRelRole(String groupId)
	{
		boolean f=false;
		Long c= this.groupUserDao.createBeanQuery(SysGroupRole.class).eq("groupId", groupId).count();
		if(c>0) f=true;
		return f;
	}
}
